<?php

namespace App\Services\User;

use App\Constant\User\UserEntity;
use App\Models\Group;
use App\Models\Setting;
use App\Models\User;
use App\Models\UserWallet;
use App\Services\Group\GroupService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class RegisterUserService
{
    public function register(array $attributes): User
    {
        DB::beginTransaction();
        try {
            $parentId = $attributes['parent_id'] ?? 0;
            /** @var User $parent */
            $parent = User::query()->find($parentId);
            // 团长直接邀请的没有上级
//            $isLeaderShip = request()->post('group_invite', 0);
//            if ($isLeaderShip) {
//                if ($parent) {
//                    if ($parent->is_group_leader) {
//                        unset($attributes['parent_id']);
//                    }
//                } else {
//                    unset($attributes['parent_id']);
//                }
//            }

            /** @var User $user */
            $user = User::query()->create((new UserEntity($attributes))->toArray());
            if (!$user) {
                throw new \Exception('用户创建失败');
            }
            $uuid = UserUuidService::encode($user->getAttribute('id'));
            $user->update(['uuid' => $uuid]);

            if ($parent) {
                // 如果邀请人数达到最低自动添加团长
                $group = Setting::get('group');
                $enabled = $group['enabled'] ?? 0;//已开启团长
//

                if ($parent->is_group_leader == 0) {
                    //上级是不是普通团长
                    //上级拉人数
                    $count = User::where('parent_id', $parent->id)->count();
                    if ($count >= $group['lowest_member']) {
                        User::where('id', $parent->id)->update(['is_group_leader' => 1]);
                        GroupService::make($parent, []);
                    }
                } else {

                    if ($parent->is_advanced_group == 0) {
                        //上级是不是高级团长
                        //上级拉人数
                        $count = User::where('parent_id', $parent->id)->count();
                        if ($count >= $group['advanced_lowest_member']) {
                            GroupService::make($parent, []);
                            User::where('id', $parent->id)->update(['is_group_leader' => 1, 'is_advanced_group' => 1]);
                        }
                    }

                }

                $parent = User::query()->find($parentId);
                /** @var Group $group */
                $group = GroupService::findLatestGroup($parent);// 获取所有上级直到上级为团长
                if ($group && $enabled) {
                    GroupService::addMember($group, $user);
                }
            }
            UserWallet::query()->create([
                'user_id' => $user->getAttribute('id'),
            ]);
            DB::commit();

            return User::query()->find($user->getAttribute('id'));
        } catch (\Exception $exception) {
            DB::rollBack();
            throw $exception;
        }
    }
}
